package com.example.threadstack;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.example.threadstack.Utils.getCurrentTime;
import static com.example.threadstack.Utils.sleepForMs;

@RestController
@RequestMapping("thread-join")
public class ThreadJoinController {
    @RequestMapping("/{timeout}")
    public String join(@PathVariable Integer timeout) throws InterruptedException {
        Thread thread = new Thread(new SleepTask(timeout));
        thread.start();
        thread.join();
        return getCurrentTime();
    }

    class SleepTask implements Runnable{
        private final Integer timeout;

        SleepTask(Integer timeout) {
            this.timeout = timeout;
        }

        @Override
        public void run() {
            sleepForMs(timeout);
        }
    }
}
